package com.eco.fanliapp.controller.app;


import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.service.ISyUserIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  用户积分
 * </p>
 *
 * @author jobob
 * @since 2019-12-24
 */
@RestController
@RequestMapping("/app/user")
public class SyUserIntegralController extends BaseController {
    @Autowired
    private ISyUserIntegralService iSyUserIntegralService;

    /**
     * 订单兑换积分
     * @param orderId
     * @return
     */
    @GetMapping("/integral/exchange")
    public ServerResponse integralExchange(String orderId) {
        AppUser user = getAppUser();
        if(null == user) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        Long orderIdLong = null;
        try {
            orderIdLong = Long.parseLong(orderId);
        }catch (Exception e){
            return ServerResponse.createByErrorMessage("请输入正确的订单号");
        }
        return iSyUserIntegralService.integralExchange(user.getUserId(),orderIdLong);
    }

    /**
     * 积分查询
     * @return
     */
    @GetMapping("/integral/query")
    public ServerResponse integralQuery() {
        AppUser user = getAppUser();
        if(null == user) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserIntegralService.integralQuery(user.getUserId());
    }

    /**
     * 积分兑换商品
     * @param goodsId
     * @return
     */
    @GetMapping("/integral/goods")
    public ServerResponse integralExchangeGoods(Long goodsId) {
        AppUser user = getAppUser();
        if(null == user) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserIntegralService.integralExchangeGoods(user.getUserId(),goodsId);
    }

    /**
     * 积分详情查询
     * @return
     */
    @GetMapping("/integral/list")
    @ResponseBody
    public ServerResponse integralList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSie, @RequestParam(defaultValue = "0") Integer type) {
        AppUser user = getAppUser();
        if(null == user) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserIntegralService.integralList(user.getUserId(),type,pageNum,pageSie);
    }

    /**
     * 积分商品查询
     * @return
     */
    @GetMapping("/integral/goodsList")
    @ResponseBody
    public ServerResponse integralGoodsList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSie) {
        AppUser user = getAppUser();
        if(null == user) {
            return ServerResponse.createByErrorCodeMessage(301, "Token不正确");
        }
        return iSyUserIntegralService.integralGoodsList(user.getUserId(),pageNum,pageSie);
    }
}
